<!-- Copyright 2015 The Chromium Authors. All rights reserved.
     Use of this source code is governed by a BSD-style license that can be
     found in the LICENSE file. -->

<link rel="import" href="chrome://resources/html/polymer.html">

<link rel="import" href="chrome://resources/cr_elements/cr_input/cr_input.html">
<link rel="import" href="chrome://resources/html/action_link.html">
<link rel="import" href="chrome://resources/html/load_time_data.html">
<link rel="import" href="chrome://resources/polymer/v1_0/iron-icon/iron-icon.html">

<link rel="import" href="../../../../gaia_auth_host/authenticator.html">
<link rel="import" href="../common/offline_ad_login.html">


<link rel="import" href="../../keyboard_utils.html">
<link rel="import" href="../../cr_ui.html">
<link rel="import" href="../../display_manager.html">
<link rel="import" href="../../components/display_manager_types.html">
<link rel="import" href="../../components/gaia_dialog.html">
<link rel="import" href="../../components/oobe_icons.html">
<link rel="import" href="../../components/oobe_types.html">
<link rel="import" href="../../components/behaviors/login_screen_behavior.html">
<link rel="import" href="../../components/behaviors/multi_step_behavior.html">
<link rel="import" href="../../components/behaviors/oobe_i18n_behavior.html">
<link rel="import" href="../../components/buttons/oobe_back_button.html">
<link rel="import" href="../../components/buttons/oobe_next_button.html">
<link rel="import" href="../../components/buttons/oobe_text_button.html">
<link rel="import" href="../../components/common_styles/common_styles.html">
<link rel="import" href="../../components/common_styles/oobe_dialog_host_styles.html">
<link rel="import" href="../../components/dialogs/oobe_adaptive_dialog.html">
<link rel="import" href="../../components/dialogs/oobe_loading_dialog.html">

<dom-module id="enterprise-enrollment-element">
  <template>
    <style include="oobe-dialog-host-styles">
      host {
        padding: 0;
      }

      #learnMoreLink {
        display: inline;
      }

      .step-message {
        display: inline-block;
        padding-top: 20px;
        text-align: start;
        vertical-align: top;
      }

      #attributePromptMessage {
        color: black;
      }
    </style>
    <!-- OAUTH GAIA DIALOG -->
    <gaia-dialog id="step-signin" class="flex" for-step="signin"
        auth-flow="{{authFlow_}}"
        is-saml-sso-visible="[[isSaml_(authFlow_)]]"
        webview-name="authView"
        on-authcompleted="onAuthCompleted_"
        on-backcancel="cancel"
        on-closesaml="cancel"
        on-identifierentered="onIdentifierEnteredMessage_"
        hide-back-button-if-cant-go-back
        on-ready="onReady">
    </gaia-dialog>

    <!-- ENROLLMENT IN PROGRESS (SPINNER) DIALOG -->
    <oobe-loading-dialog title-key="oauthEnrollScreenTitle"
        id="step-working" for-step="working">
      <iron-icon slot="icon" icon="oobe-32:enterprise"></iron-icon>
    </oobe-loading-dialog>

    <!-- ACCOUNT CHECK IN PROGRESS (SPINNER) DIALOG -->
    <oobe-loading-dialog title-key="enrollmentAccountCheckTitle"
        id="step-checking" for-step="checking">
      <iron-icon slot="icon" icon="oobe-32:enterprise"></iron-icon>
    </oobe-loading-dialog>

    <oobe-loading-dialog id="step-tpm-checking" for-step="tpm-checking"
        title-key="TPMCheckTitle" subtitle-key="TPMCheckSubtitle"
        can-cancel on-cancel-loading="onTPMCheckCanceled_">
      <iron-icon slot="icon" icon="oobe-32:enterprise"></iron-icon>
    </oobe-loading-dialog>

    <offline-ad-login-element id="step-ad-join" is-domain-join
        for-step="ad-join"
        class="fit"
        ad-welcome-message-key="oauthEnrollAdDomainJoinWelcomeMessage">
    </offline-ad-login-element>

    <!--  ERROR DIALOG -->
    <!-- There are three kinds of errors:
            - General
            - AttributePrompt
            - ActiveDirectoryJoin

        They share the same structure but have different bottom buttons.
            General: [ Back, Skip/Manual Enrollment, Try Again ]
            AttributePrompt: [ 'Done' ]
            ActiveDirectoryJoin: [ 'Try Again' ]
    -->
    <oobe-adaptive-dialog id="step-error" role="alert"
        for-step="error, attribute-prompt-error, active-directory-join-error">
      <iron-icon slot="icon" icon="oobe-32:warning"></iron-icon>
      <h1 slot="title">[[i18nDynamic(locale, 'oauthEnrollErrorTitle')]]</h1>
      <div id="errorMsg" slot="subtitle">[[errorText_]]</div>
      <div slot="content"
          class="flex layout vertical center center-justified">
        <img src="images/error.svg" class="oobe-illustration">
      </div>
      <div slot="back-navigation">
        <!-- BACK - Shown when the generic cancel button is hidden -->
        <oobe-back-button id="errorCancelBackButton"
            hidden$="[[isGenericCancel_(isAutoEnroll_, isForced_)]]"
            on-click="cancel"></oobe-back-button>
      </div>
      <div slot="bottom-buttons">
        <!-- BOTTOM BUTTONS: GENERIC ERROR -->
        <div id="genericErrorBottomButtons"
            class="flex layout horizontal end-justified" for-step="error">
          <!-- GENERAL CANCEL BUTTON (SKIP / ENROLL MANUALLY) -->
          <oobe-text-button id="errorGenericCancelButton"
              hidden$="[[!isGenericCancel_(isAutoEnroll_, isForced_)]]"
              on-click="cancel"  inverse$="[[!canRetryAfterError_]]"
              text-key="[[getCancelButtonLabel_(locale, isAutoEnroll_)]]">
          </oobe-text-button>

          <!-- TRY AGAIN BUTTON -  -->
          <oobe-text-button inverse id="errorRetryButton"
            hidden$="[[!canRetryAfterError_]]"
            on-click="doRetry_" class="focus-on-show"
            text-key="oauthEnrollRetry">
          </oobe-text-button>
        </div>

        <!-- BOTTOM BUTTONS: ACTIVE DIRECTORY JOIN ERROR-->
        <div for-step="active-directory-join-error">
          <oobe-text-button inverse id="adRetryButton"
              on-click="onAdJoinErrorRetry_" class="focus-on-show"
              text-key="oauthEnrollRetry">
          </oobe-text-button>
        </div>

        <!-- BOTTOM BUTTONS: ATTRIBUTE ERROR -->
        <div for-step="attribute-prompt-error">
          <oobe-text-button inverse id="attributeErrorButton"
              on-click="onEnrollmentFinished_" class="focus-on-show"
              text-key="oauthEnrollDone">
          </oobe-text-button>
        </div>

      </div>
    </oobe-adaptive-dialog>

    <!-- SUCCESS DIALOG -->
    <oobe-adaptive-dialog id="step-success" for-step="success" role="alert"
        footer-shrinkable>
      <iron-icon slot="icon" icon="oobe-32:enterprise"></iron-icon>
      <h1 slot="title">
        [[i18nDynamic(locale, 'oauthEnrollSuccessTitle')]]
      </h1>
      <!-- Hide the subtitle if the domain could not be determined. -->
      <div slot="subtitle" class="self-start" hidden="[[isMeet_]]">
        <div hidden="[[isEmpty_(domainManager_)]]">
          [[successText_(locale, deviceName_, domainManager_)]]
        </div>
      </div>
      <div slot="content" class="flex layout vertical center center-justified">
        <img src="images/enrollment_complete.svg"
            class="oobe-illustration" hidden="[[isMeet_]]">
        <img src="images/cfm/enrollment.svg"
            class="oobe-illustration" hidden="[[!isMeet_]]">
      </div>
      <div slot="bottom-buttons">
        <oobe-text-button inverse id="successDoneButton"
            text-key="oauthEnrollDone" on-click="onEnrollmentFinished_"
            class="focus-on-show"></oobe-text-button>
      </div>
    </oobe-adaptive-dialog>

    <!-- ATTRIBUTE PROMPT DIALOG -->
    <oobe-adaptive-dialog id="step-attribute-prompt"
        for-step="attribute-prompt">
      <iron-icon slot="icon" icon="oobe-32:enterprise"></iron-icon>
      <h1 slot="title">
        [[i18nDynamic(locale, 'oauthEnrollScreenTitle')]]
      </h1>
      <p slot="subtitle">
        [[i18nDynamic(locale, 'oauthEnrollDeviceInformation')]]
      </p>
      <div slot="content" class="flex layout vertical">
        <div class="step-message">
          <span id="attributePromptMessage">
            [[i18nDynamic(locale, 'oauthEnrollAttributeExplanation')]]
          </span>
          <a id="learnMoreLink" class="oobe-local-link" is="action-link"
              on-click="onLearnMore_">
            [[i18nDynamic(locale, 'oauthEnrollExplainAttributeLink')]]
          </a>
        </div>
        <div class="oobe-form">
          <cr-input id="assetId" type="text" value="{{assetId_}}"
              class="focus-on-show"
              label="[[i18nDynamic(locale, 'enrollmentAssetIdLabel')]]">
          </cr-input>
          <cr-input id="location" type="text" value="{{deviceLocation_}}"
              label="[[i18nDynamic(locale, 'enrollmentLocationLabel')]]">
          </cr-input>
        </div>
      </div>
      <div slot="bottom-buttons">
        <oobe-text-button id="attributesSkip"
            text-key="oauthEnrollSkip" on-click="skipAttributes_">
        </oobe-text-button>
        <oobe-next-button id="attributesSubmit"
            on-click="submitAttributes_"></oobe-next-button>
      </div>
    </oobe-adaptive-dialog>
  </template>
  <script src="enterprise_enrollment.js"></script>
</dom-module>
